package com.gcit.seniorWish.web.modular.portal.service.impl;

import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.gcit.seniorWish.core.exception.GunsException;
import com.gcit.seniorWish.web.common.constant.cache.Cache;
import com.gcit.seniorWish.web.common.constant.cache.CacheKey;
import com.gcit.seniorWish.web.common.constant.enums.ExamineeStatusEnum;
import com.gcit.seniorWish.web.common.constant.enums.SeniorWishStatusEnum;
import com.gcit.seniorWish.web.common.constant.factory.ConstantFactory;
import com.gcit.seniorWish.web.common.persistence.dao.SeniorExamineeMapper;
import com.gcit.seniorWish.web.common.persistence.dao.SeniorWishMapper;
import com.gcit.seniorWish.web.common.persistence.model.EnrollSchool;
import com.gcit.seniorWish.web.common.persistence.model.EnrollSchoolBatchMajor;
import com.gcit.seniorWish.web.common.persistence.model.SeniorExaminee;
import com.gcit.seniorWish.web.common.persistence.model.SeniorWish;
import com.gcit.seniorWish.web.core.exception.BizExceptionEnum;
import com.gcit.seniorWish.web.modular.examinee.dao.SeniorExamineeDao;
import com.gcit.seniorWish.web.modular.examinee.dao.SeniorWishDao;
import com.gcit.seniorWish.web.modular.examinee.service.ISeniorExamineeService;
import com.gcit.seniorWish.web.modular.portal.service.ISeniorWishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.*;

/**
 * <p>
 * 志愿表 服务实现类
 * </p>
 *
 * @author gaozhichang
 * @since 2018-03-22
 */
@Service
public class SeniorWishServiceImpl extends ServiceImpl<SeniorWishMapper, SeniorWish> implements ISeniorWishService {

	@Resource
	private SeniorExamineeMapper seniorExamineeMapper;

	@Autowired
	private ISeniorExamineeService seniorExamineeService;

	@Resource
	private SeniorExamineeDao seniorExamineeDao;

	@Resource
	private SeniorWishDao seniorWishDao;

	/**
	 * 保存志愿
	 * <p>
	 * 需要删除原本缓存的考生信息
	 *
	 * @param seniorWish 志愿信息
	 * @author gaozhichang
	 * @date 2018-03-22 下午 5:12
	 */
	@Override
	@Transactional
	@CacheEvict(value = Cache.BUSS, key = "'" + CacheKey.EXAMINEE_INFO_BY_EXAM_CARD + "'+#seniorWish.examCardNumber")
	public void mergeSeniorWish(SeniorWish seniorWish) throws IllegalAccessException {
		SeniorExaminee examinee = seniorExamineeService.getExamineeByExamCardNumberWithSchoolClassName(seniorWish.getExamCardNumber());
		if (examinee == null) {
			throw new GunsException(BizExceptionEnum.EXAMINEE_NOT_EXIST);
		}
		//判断该考生是否已经提交过
		if (examinee.getConfirmStatus() == ExamineeStatusEnum.ALREADY_CONFIRM.getValue()) {
			throw new GunsException(BizExceptionEnum.SENIOR_WISH_ALREADY_CONFIRM);
		}
		seniorWish.setExamineeId(examinee.getId());
		if (StrUtil.isBlank(seniorWish.getId())) {
			baseMapper.insert(seniorWish);
		} else {
			baseMapper.updateAllColumnById(seniorWish);
		}
		//同步修改为senior_wish_bureau表(senior_wish_bureau是招生办指定需要的表，字段必须按照给定的要求来)
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("examinee_id",seniorWish.getExamineeId());
		map.put("fqpc0sf",seniorWish.getBatch1QuitFlag());
		map.put("XX01",ConstantFactory.me().getEnrollSchoolById(seniorWish.getBatch1School1Id()).getSchoolCode());
		map.put("fqpc0zb",seniorWish.getBatch2QuitFlag());
		map.put("QTXX21",ConstantFactory.me().getEnrollSchoolById(seniorWish.getBatch2School1Id()).getSchoolCode());
		map.put("QTXX22",ConstantFactory.me().getEnrollSchoolById(seniorWish.getBatch13School1Id()).getSchoolCode());
		map.put("QTXX23",ConstantFactory.me().getEnrollSchoolById(seniorWish.getBatch13School2Id()).getSchoolCode());
		map.put("fqpc0pg",seniorWish.getBatch3QuitFlag());
		map.put("XX11",ConstantFactory.me().getEnrollSchoolById(seniorWish.getBatch3School1Id()).getSchoolCode());
		map.put("fqpc1",seniorWish.getBatch4a5QuitFlag());
		map.put("XX13",ConstantFactory.me().getEnrollSchoolById(seniorWish.getBatch4School1Id()).getSchoolCode());
		map.put("XX14",ConstantFactory.me().getEnrollSchoolById(seniorWish.getBatch4School2Id()).getSchoolCode());
		map.put("XX15",ConstantFactory.me().getEnrollSchoolById(seniorWish.getBatch4School3Id()).getSchoolCode());
		map.put("XX16",ConstantFactory.me().getEnrollSchoolById(seniorWish.getBatch5School1Id()).getSchoolCode());
		map.put("ZY161",ConstantFactory.me().getEnrollMajorById(seniorWish.getBatch5School1Major1Id()).getMajorCode());
		map.put("QTXX24",ConstantFactory.me().getEnrollSchoolById(seniorWish.getBatch5School2Id()).getSchoolCode());
		map.put("QTZY241",ConstantFactory.me().getEnrollMajorById(seniorWish.getBatch5School2Major1Id()).getMajorCode());
		map.put("fqpc2",seniorWish.getBatch6QuitFlag());
		map.put("XX17",ConstantFactory.me().getEnrollSchoolById(seniorWish.getBatch6School1Id()).getSchoolCode());
		map.put("XX18",ConstantFactory.me().getEnrollSchoolById(seniorWish.getBatch6School2Id()).getSchoolCode());
		map.put("fqpc3",seniorWish.getBatch7QuitFlag());
		map.put("XX33",ConstantFactory.me().getEnrollSchoolById(seniorWish.getBatch7School1Id()).getSchoolCode());
		map.put("fqpc4",seniorWish.getBatch8a9a10QuitFlag());
		map.put("YSXX20",ConstantFactory.me().getEnrollSchoolById(seniorWish.getBatch8School1Id()).getSchoolCode());
		map.put("YSZY201",ConstantFactory.me().getEnrollMajorById(seniorWish.getBatch8School1Major1Id()).getMajorCode());
		map.put("YSZY202",ConstantFactory.me().getEnrollMajorById(seniorWish.getBatch8School1Major2Id()).getMajorCode());
		map.put("YSFC20",seniorWish.getBatch8School1MajorAllocateFlag());
		map.put("GZXX21",ConstantFactory.me().getEnrollSchoolById(seniorWish.getBatch9School1Id()).getSchoolCode());
		map.put("GZZY211",ConstantFactory.me().getEnrollMajorById(seniorWish.getBatch9School1Major1Id()).getMajorCode());
		map.put("GZZY212",ConstantFactory.me().getEnrollMajorById(seniorWish.getBatch9School1Major2Id()).getMajorCode());
		map.put("GZZY213",ConstantFactory.me().getEnrollMajorById(seniorWish.getBatch9School1Major3Id()).getMajorCode());
		map.put("GZZY214",ConstantFactory.me().getEnrollMajorById(seniorWish.getBatch9School1Major4Id()).getMajorCode());
		map.put("GZZY215",ConstantFactory.me().getEnrollMajorById(seniorWish.getBatch9School1Major5Id()).getMajorCode());
		map.put("GZFC21",seniorWish.getBatch9School1MajorAllocateFlag());
		map.put("GZXX22",ConstantFactory.me().getEnrollSchoolById(seniorWish.getBatch9School2Id()).getSchoolCode());
		map.put("GZZY221",ConstantFactory.me().getEnrollMajorById(seniorWish.getBatch9School2Major1Id()).getMajorCode());
		map.put("GZZY222",ConstantFactory.me().getEnrollMajorById(seniorWish.getBatch9School2Major2Id()).getMajorCode());
		map.put("GZZY223",ConstantFactory.me().getEnrollMajorById(seniorWish.getBatch9School2Major3Id()).getMajorCode());
		map.put("GZZY224",ConstantFactory.me().getEnrollMajorById(seniorWish.getBatch9School2Major4Id()).getMajorCode());
		map.put("GZZY225",ConstantFactory.me().getEnrollMajorById(seniorWish.getBatch9School2Major5Id()).getMajorCode());
		map.put("GZFC22",seniorWish.getBatch9School2MajorAllocateFlag());
		map.put("GZXX23",ConstantFactory.me().getEnrollSchoolById(seniorWish.getBatch9School3Id()).getSchoolCode());
		map.put("GZZY231",ConstantFactory.me().getEnrollMajorById(seniorWish.getBatch9School3Major1Id()).getMajorCode());
		map.put("GZZY232",ConstantFactory.me().getEnrollMajorById(seniorWish.getBatch9School3Major2Id()).getMajorCode());
		map.put("GZZY233",ConstantFactory.me().getEnrollMajorById(seniorWish.getBatch9School3Major3Id()).getMajorCode());
		map.put("GZZY234",ConstantFactory.me().getEnrollMajorById(seniorWish.getBatch9School3Major4Id()).getMajorCode());
		map.put("GZZY235",ConstantFactory.me().getEnrollMajorById(seniorWish.getBatch9School3Major5Id()).getMajorCode());
		map.put("GZFC23",seniorWish.getBatch9School3MajorAllocateFlag());
		map.put("FCQTGZ",seniorWish.getBatch9SchoolAllocateFlag());
		map.put("ZZXX21",ConstantFactory.me().getEnrollSchoolById(seniorWish.getBatch10School1Id()).getSchoolCode());
		map.put("ZZZY211",ConstantFactory.me().getEnrollMajorById(seniorWish.getBatch10School1Major1Id()).getMajorCode());
		map.put("fqpc5",seniorWish.getBatch11QuitFlag());
		map.put("XX35",ConstantFactory.me().getEnrollSchoolById(seniorWish.getBatch11School1Id()).getSchoolCode());
		map.put("XX36",ConstantFactory.me().getEnrollSchoolById(seniorWish.getBatch11School2Id()).getSchoolCode());
		map.put("QTXX15",ConstantFactory.me().getEnrollSchoolById(seniorWish.getBatch11School3Id()).getSchoolCode());
		map.put("fqpcqt",seniorWish.getBatch12QuitFlag());
		map.put("QTXX16",ConstantFactory.me().getEnrollSchoolById(seniorWish.getBatch12School1Id()).getSchoolCode());
		map.put("QTXX17",ConstantFactory.me().getEnrollSchoolById(seniorWish.getBatch12School2Id()).getSchoolCode());
		map.put("QTXX18",ConstantFactory.me().getEnrollSchoolById(seniorWish.getBatch12School3Id()).getSchoolCode());
		map.put("QTXX19",ConstantFactory.me().getEnrollSchoolById(seniorWish.getBatch12School4Id()).getSchoolCode());
		seniorWishDao.replaceSeniorWishBureau(map);

		//设置填报志愿状态为已填报(只更新填写状态，防止覆盖其他修改信息)
		SeniorExaminee examineeUpdate = new SeniorExaminee();
		examineeUpdate.setId(examinee.getId());
		examineeUpdate.setWriteStatus(ExamineeStatusEnum.ALREADY_WRITE.getValue());
		//初始认为所有都放弃，只要判断有一个未放弃，就认为没有放弃志愿
		examineeUpdate.setIsAbandoned(ExamineeStatusEnum.ALREADY_ABANDON.getValue());
		//判断志愿中是否全部放弃
		Field[] fields = seniorWish.getClass().getDeclaredFields();
		for (int i = 0; i < fields.length; i++) {
			Field field = fields[i];
			//设置可以访问私有变量
			field.setAccessible(true);
			//查询该字段是否为不放弃，只要有不放弃的标签。设置考生放弃状态为未放弃
			if (ReUtil.findAll("QuitFlag", field.getName(), 0).size() > 0
					&& ((Integer) field.get(seniorWish)) == SeniorWishStatusEnum.NOT_QUIT.getValue()) {
				examineeUpdate.setIsAbandoned(ExamineeStatusEnum.NOT_YET_ABANDON.getValue());
				break;
			}
		}
		seniorExamineeMapper.updateById(examineeUpdate);
	}

	/**
	 * 查询学生志愿详情
	 *
	 * @param examCardNumber 考生准考证号
	 * @author gaozhichang
	 * @date 2018-03-23 下午 5:24
	 */
	@Override
	public SeniorWish seniorWishDetail(String examCardNumber) throws IllegalAccessException {
		//查询学生id
		SeniorExaminee examinee = seniorExamineeService.getExamineeByExamCardNumberWithSchoolClassName(examCardNumber);
		if (examinee == null) {
			throw new GunsException(BizExceptionEnum.EXAMINEE_NOT_EXIST);
		}
		SeniorWish seniorWish = this.selectOne(Condition.create().eq("examinee_id", examinee.getId()));
		if (seniorWish == null) {
			throw new GunsException(BizExceptionEnum.SENIOR_WISH_NOT_EXIST);
		}
		setEnrollSchoolNameAndMajorName(seniorWish);
		return seniorWish;
	}


	/**
	 * 确认志愿提交
	 *
	 * @param examCardNumber 准考证号
	 * @author gaozhichang
	 * @date 2018-03-25 下午 2:53
	 */
	@Override
	@Transactional
	@CacheEvict(value = Cache.BUSS, key = "'" + CacheKey.EXAMINEE_INFO_BY_EXAM_CARD + "'+#examCardNumber")
	public void confirmSeniorWish(String examCardNumber) {
		SeniorExaminee examinee = seniorExamineeService.getExamineeByExamCardNumberWithSchoolClassName(examCardNumber);
		if (examinee == null) {
			throw new GunsException(BizExceptionEnum.EXAMINEE_NOT_EXIST);
		}
		SeniorExaminee examineeUpdate = new SeniorExaminee();
		examineeUpdate.setId(examinee.getId());
		examineeUpdate.setConfirmStatus(ExamineeStatusEnum.ALREADY_CONFIRM.getValue());
		seniorExamineeMapper.updateById(examineeUpdate);
	}

	/**
	 * 根据考生id列表查询考生信息（包括志愿信息）
	 *
	 * @param examineeIdList 考生id列表
	 * @author gaozhichang
	 * @date 2018-03-27 上午 9:40
	 */
	@Override
	public List<SeniorExaminee> listSeniorExamineeWithSeniorWish(List<String> examineeIdList) {
		List<SeniorExaminee> examineeList = seniorExamineeDao.listSeniorExamineeWithSeniorWish(examineeIdList);
		for (SeniorExaminee seniorExaminee : examineeList) {
			seniorExaminee.setSeniorSchoolName(ConstantFactory.me().getSeniorSchoolById(seniorExaminee.getSeniorSchoolId()).getSchoolName());
			seniorExaminee.setSeniorClassName(ConstantFactory.me().getSeniorSchoolClassById(seniorExaminee.getSeniorClassId()).getSenoirClassName());
			SeniorWish seniorWish = seniorExaminee.getSeniorWish();
			if (seniorWish != null) {
				setEnrollSchoolNameAndMajorName(seniorWish);
			}
		}
		return examineeList;
	}

	/**
	* 设置报考学校名称和专业名称
	* @param seniorWish 需要设置的志愿信息
	* @author gaozhichang
	* @date 2018-03-27 上午 11:59
	*/
	private void setEnrollSchoolNameAndMajorName(SeniorWish seniorWish) {
		//设置学校名称
		Field[] fields = seniorWish.getClass().getDeclaredFields();
		for (int i = 0; i < fields.length; i++) {
			Field field = fields[i];
			//设置可以访问私有变量
			field.setAccessible(true);
			//查询该字段是否为报考学校名称
			if (ReUtil.findAll("School\\dName", field.getName(), 0).size() > 0) {
				String schoolId = ReflectUtil.invoke(seniorWish, "get" + StrUtil.upperFirst(field.getName()).replace("Name", "Id"));
				if (StrUtil.isNotBlank(schoolId)) {
					EnrollSchool enrollSchool = ConstantFactory.me().getEnrollSchoolById(schoolId);
					String schoolName =enrollSchool.getSchoolCode()+":"+enrollSchool.getSchoolName();
					if(StrUtil.isNotBlank(schoolName)){
						ReflectUtil.invoke(seniorWish, "set" + StrUtil.upperFirst(field.getName()), schoolName);
					}
				}
			}
//			查询该字段是否为报考专业名称
			if (ReUtil.findAll("Major\\dName", field.getName(), 0).size() > 0) {
				String majorId = ReflectUtil.invoke(seniorWish, "get" + StrUtil.upperFirst(field.getName()).replace("Name", "Id"));
				if (StrUtil.isNotBlank(majorId)) {
					EnrollSchoolBatchMajor enrollMajor = ConstantFactory.me().getEnrollMajorById(majorId);
					EnrollSchool enrollSchool = ConstantFactory.me().getEnrollSchoolById(enrollMajor.getEnrollSchoolId());
					String majorName = enrollSchool.getSchoolCode()+"-"+enrollMajor.getMajorCode()+":"+enrollMajor.getMajorName();
					if(StrUtil.isNotBlank(majorName)){
						ReflectUtil.invoke(seniorWish, "set" + StrUtil.upperFirst(field.getName()), majorName);
					}
				}
			}
		}
	}



}
